package data.room;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import javax.swing.JOptionPane;

import miscellaneous.Storage;
import data.Appointment;
import data.Data;

/**
 * The reservation for an appointment
 * @author daniel
 */

public class Reservation extends Data{

	//Per now it is possible to reserve an appointment over several days
	private Appointment appointment;
	private Calendar startTime;
	private Calendar endTime;
	private Room room;
	
	/**
	 * The constructor
	 * @param start The start of the reservation
	 * @param end The end of the reservation
	 */
	public Reservation(Calendar start, Calendar end) {
		super();
		startTime = new GregorianCalendar(Locale.FRANCE);
		endTime = new GregorianCalendar(Locale.FRANCE);
		if(start != null) {
			setStartTime(start);
		}
		if(end != null) {
			setEndTime(end);
		}
	}

	/**
	 * The constructor
	 * @param start The start of the reservation
	 * @param end The end of the reservation
	 * @param room Where the reservation is at (optional)
	 */
	public Reservation(Calendar start, Calendar end,
			Room room) {
		this(start, end);
		this.room = room;
	}

	/**
	 * Updates all the information at once.
	 * @param start The start time of the reservation
	 * @param end The end time of the reservation
	 * @param room The room of this reservation
	 */
	public void updateAll(Calendar start, Calendar end,
			Room room) {
		if(appointment.getOwner() != Storage.getloggedInUser()) {
			JOptionPane.showMessageDialog(null, "Kun eier/møteleder kan endre dette møtet - flytt dialogbokskoden vekk fra reservation");
		} else {
			setStartTime(start);
			setEndTime(end);
			setRom(room);

			changeSupport.firePropertyChange("update", null, null);
		}
	}
	
	public Calendar getStartTime() {
		return startTime;
	}

	public void setStartTime(Calendar startTime) {
		this.startTime = startTime;
		if(!checkEndAfterStart()) {
			this.endTime = (Calendar) startTime.clone();
		}
	}

	public Calendar getEndTime() {
		return endTime;
	}

	public Appointment getAppointment() {
		return appointment;
	}

	public void setAppointment(Appointment appointment) {
		this.appointment = appointment;
	}

	public void setEndTime(Calendar endTime) {
		this.endTime = endTime;
		if(!checkEndAfterStart()) {
			if(this.startTime != null) {
				this.endTime = (Calendar) this.startTime;
			} else {
				this.startTime = (Calendar) this.endTime;
			}
		}
	}

	public Room getRoom() {
		return room;
	}

	public void setRom(Room room) {
		this.room = room;
	}

	/**
	 * Checks start is before end in this
	 * reservation
	 * @return true if start is before end
	 */
	private boolean checkEndAfterStart() {
		if(this.startTime != null && this.endTime != null) {
			if(this.startTime.compareTo(this.endTime) <= 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Calculates the duration of a meeting in minutes
	 * @return The number of hours the meeting will last
	 */
	public int getDuration() {
		if(startTime != null && endTime != null) {
			return (int)((endTime.getTimeInMillis() - startTime.getTimeInMillis())/(1000*60));
		}
		return Integer.MAX_VALUE;
	}
}